package com.ljl.org.controller;


import com.ljl.basic.annotation.PreAuthorize;
import com.ljl.basic.util.AjaxResult;
import com.ljl.basic.util.PageList;
import com.ljl.org.domain.Department;
import com.ljl.org.query.DepartmentQuery;
import com.ljl.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    // 查询单个
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.findById(id);
    }

    // 查询所有
    @GetMapping()
    @ApiOperation(value = "查询全部" )
    @PreAuthorize(name = "部门查询",sn = "department:getAll")
    public List<Department> queryAll(){
        return departmentService.findAll();
    }

    // 删除
    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    @PreAuthorize(name = "部门删除",sn = "department:delete")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            departmentService.del(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }

    // 分页和高级查询
    @PostMapping
    @ApiOperation(value = "分页查询" )
    @PreAuthorize(name = "部门列表",sn = "department:list")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    // 修改或添加
    @PutMapping
    @ApiOperation(value = "添加或修改" )
    @PreAuthorize(name = "部门保存",sn = "department:save")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() != null) {
                departmentService.update(department);
            } else {
                departmentService.save(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }
    // 批量删除
    @PatchMapping
    @PreAuthorize(name = "部门批删",sn = "department:batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙请稍后重试！！");
        }
    }

    @GetMapping("/tree")
    @PreAuthorize(name = "部门树",sn = "department:tree")
    public List<Department> deptTree(){
        return departmentService.deptTree();
    }

}
